﻿<%@ Page Language="C#" %> 

<!DOCTYPE html>
<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //var list =new LambdaToSql.SqlClient().Context.TableList;
        //foreach (var table in list)
        //{ 
        //    Create("Request.txt", "Request.cs", table.Name, table.Comments, table.Columns);
        //    Create("Response.txt", "Response.cs", table.Name, table.Comments, table.Columns);
        //}
    }


    /// <summary>
    /// 生成首页代码
    /// </summary>
    /// <param name="tbName">表名称</param>
    /// <param name="tbNotes">表注释</param>
    private void Create(string template, string writeName, string tbName, string tbNotes, List<CommonLib.Model.TableColumn> Columns)
    {
        string SavedFolder = "d:\\class\\" + tbName + "\\";

        if (SavedFolder.Substring(SavedFolder.Length - 1, 1) != "\\")
        {
            SavedFolder += "\\";
        }
        if (!Directory.Exists(SavedFolder))
        {
            Directory.CreateDirectory(SavedFolder);
        }

        string writePath = SavedFolder + writeName;
        string str = txt_read("/Tools/CreateClass/Template/" + template);

        str = str.Replace("$表名$", tbName);
        str = str.Replace("$表注释$", tbNotes);

        var AddUpdate = "";
        foreach (var item in Columns)
        {
            if (item.Name != "ID" && item.Name != "AddTime" && item.Name != "IsDelete" && item.Name != "OrgLevels" && item.Name != "CityLevels" && item.Name != "AddUserID" && item.Name != "LastUserId" && item.Name != "LastTime")
            {
                AddUpdate += string.Format(@" /// <summary>
        /// {0}
        /// </summary>
        [DataMember]
        public {1} {2} {{ get; set; }}" + "\r\n\r\n", item.Comments, GetType(item.Type), item.Name);

            }
        }
        str = str.Replace("$AddUpdate$", AddUpdate);

        txt_write(writePath, str);
        Response.Write(tbName + "<br />");
        Response.Flush();
    }
    public string GetType(string name)
    {
        var type = "string";
        switch (name)
        {
            case "uniqueidentifier":
                {
                    type = "int?";
                    break;
                }
            case "int":
                {
                    type = "Nullable<int>";
                    break;
                }
            case "bit":
                {
                    type = "Nullable<bool>";
                    break;
                }
            case "datetime":
                {
                    type = "Nullable<System.DateTime>";
                    break;
                }
            case "decimal":
                {
                    type = "Nullable<decimal>";
                    break;
                }
        }
        return type;
    }

    #region 文件读写

    /// <summary>
    /// 文件写入方法
    /// </summary>
    /// <param name="path">文件路径:相对路径</param>
    /// <param name="str">写入内容</param>
    /// <param name="encoding">编码格式</param>
    public static void txt_write(string path, string str, bool append = false, string encoding = "utf-8")
    {
        System.Text.Encoding code = System.Text.Encoding.GetEncoding(encoding);
        System.IO.StreamWriter sw = null;
        try
        {
            sw = new System.IO.StreamWriter(path, append, code);
            sw.Write(str);
            sw.Flush();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            sw.Close();
            sw.Dispose();
        }
    }

    /// <summary>
    /// 文件读取方法
    /// </summary>
    /// <param name="path">文件路径:相对路径</param>
    /// <param name="encoding">编码格式</param>
    public static string txt_read(string path, string encoding = "utf-8")
    {
        //源码是替换掉模板中的特征字符            
        string Path = System.Web.HttpContext.Current.Server.MapPath(path);
        Encoding code = Encoding.GetEncoding(encoding);
        System.IO.StreamReader Sr = null;

        string str = "";

        //读取
        try
        {
            Sr = new StreamReader(Path, code);
            str = Sr.ReadToEnd();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            Sr.Close();
        }
        return str;
    }

    #endregion 文件读写
</script>


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="生成扩展实体类" />
        </div>
    </form>
</body>
</html>
